﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using SAAC.DTO;
using System.Data;


namespace SAAC.DAL
{
	public class DisciplinaLivreDAL : DALBase
	{

		public DisciplinaLivreDTO Select(DisciplinaLivreDTO dto, string order = null)
		{
			try
			{
				string sql = "CALL sp_disciplinaLivre_sel(@id,@codigoDisciplina,@codigoDepartamento,@order,@limit);";

				MySqlCommand cmd = new MySqlCommand(sql, con);
				cmd.Parameters.AddWithValue("@id", dto.Id);
				cmd.Parameters.AddWithValue("@codigoDisciplina", dto.CodigoDisciplina);
				cmd.Parameters.AddWithValue("@codigoDepartamento", dto.CodigoDepartamento);
				cmd.Parameters.AddWithValue("@order", order);
				cmd.Parameters.AddWithValue("@limit", 1);

				con.Open();
				MySqlDataReader dr;

				dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

				if (dr.Read())
				{
					dto.Id = Convert.ToInt32(dr["id"]);
					dto.CodigoDisciplina = dr["codigoDisciplina"].ToString();
					dto.CodigoDepartamento = dr["codigoDepartamento"].ToString();
				}
				else dto = null;

				return dto;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public List<DisciplinaLivreDTO> SelectAll(DisciplinaLivreDTO dto, string order = null, int limit = 1000)
		{
			try
			{
				string sql = "CALL sp_disciplinaLivre_sel(@id,@codigoDisciplina,@codigoDepartamento,@order,@limit);";

				var lista = new List<DisciplinaLivreDTO>();

				MySqlCommand cmd = new MySqlCommand(sql, con);
				cmd.Parameters.AddWithValue("@id", dto.Id);
				cmd.Parameters.AddWithValue("@codigoDisciplina", dto.CodigoDisciplina);
				cmd.Parameters.AddWithValue("@codigoDepartamento", dto.CodigoDepartamento);
				cmd.Parameters.AddWithValue("@order", order);
				cmd.Parameters.AddWithValue("@limit", limit);

				con.Open();
				MySqlDataReader dr;

				dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

				while (dr.Read())
				{
					var item = new DisciplinaLivreDTO();
					item.Id = Convert.ToInt32(dr["id"]);
					item.CodigoDisciplina = dr["codigoDisciplina"].ToString();
					item.CodigoDepartamento = dr["codigoDepartamento"].ToString();
					lista.Add(item);
				}

				return lista;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}


		public void Insert(DisciplinaLivreDTO dto)
		{
			try
			{
				String sql = @"CALL sp_disciplinaLivre_ins(@id,@codigoDisciplina,@codigoDepartamento);";

				MySqlCommand cmd = new MySqlCommand(sql, con);

				cmd.Parameters.AddWithValue("@id", dto.Id);
				cmd.Parameters.AddWithValue("@codigoDisciplina", dto.CodigoDisciplina);
				cmd.Parameters.AddWithValue("@codigoDepartamento", dto.CodigoDepartamento);

				con.Open();
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public void Update(DisciplinaLivreDTO dto)
		{
			try
			{
				String sql = "CALL sp_disciplinaLivre_upd(@id,@codigoDisciplina,@codigoDepartamento);";

				MySqlCommand cmd = new MySqlCommand(sql, con);

				cmd.Parameters.AddWithValue("@id", dto.Id);
				cmd.Parameters.AddWithValue("@codigoDisciplina", dto.CodigoDisciplina);
				cmd.Parameters.AddWithValue("@codigoDepartamento", dto.CodigoDepartamento);

				con.Open();
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public void Delete(DisciplinaLivreDTO dto)
		{
			try
			{
				String sql = "CALL sp_disciplinaLivre_del(@id);";

				MySqlCommand cmd = new MySqlCommand(sql, con);

				cmd.Parameters.AddWithValue("@id", dto.Id);
				con.Open();
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}
	}
}
